<?php
/**
 * 统计
 *
 * @author 孙晓晔
 * @version $Id: Ad.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/AdminUtil.class.php');

class Tj extends AdminUtil {
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function _group_tj2($sql) {
		$db = newdb($this->dsn_r);
		$result = $db->query($sql);

		$data = array();
		$count = 1;
		$xx = 0;
		$total = 0;
		$max_result = intval($this->result_s);
		while ($row = $db->fetch_array($result)) {
			$total += $row['c'];
			if ($count >= $max_result) {
				$xx += $row['c'];
				continue;
			}

			if ($row['c'] == 0) {
				continue;
			}

			$row = $this->_name($row, $this->groupby_s);
			$data[] = $row;
			$count++;
		}

		if ($xx > 0) {
			$data[] = array('a'=>'其它', 'c'=>$xx);
		}

		$aa = array();
		while (list($k, $v) = each($data)) {
			$v['p'] = number_format(($v['c'] / $total) * 100, 2, '.', '') . "%";
			
			$aa[] = $v;
		}

		$ret['data'] = $aa;
		$ret['total'] = $total;
		
		return $ret;
	}
	
	function _tj_list($a) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE %s ORDER BY %s";		
		$sql = sprintf($fmt, $this->tbl, $a['w'], $a['b']);
		echo $sql . "\n";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
		
			$data[] = $row;
		}
		
		$ret['data'] = $data;
		
		return $ret;
	}
	
	function _tj_df($d, $f) {
		$fmt = "DATE_FORMAT(%s, '%s') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%s')";
		
		return sprintf($fmt, $d, $f, $f);
	}
	
	function _tj_df_not($f) {
		$fmt = "DATE_FORMAT(createtime, '%s') <> DATE_FORMAT(modifytime, '%s')";
		
		return sprintf($fmt, $f, $f);
	}
	
	function _tj_cc($controller, $cc) {
		$list = array();
		$i = 0;
		while (list($k, $v) = each($cc)) {
			$row = $this->_tj_list($v);
			$row['title'] = $k;
			
			$list[] = $row;
			$i += count($row['data']);
		}
		
		if ($i == 0) {
			echo "222";
			return;
		}
		
		$controller->assign_by_ref('list', $list);
	}
	
	function _tj_html($controller, $request, $tpl) {
		$request['tpl'] = "html4";
		
		return $controller->sunxy($request, $tpl);
	}
	
	function _tj_mail($from, $bb, $subject, $body) {
		if (!$bb || count($bb) == 0) {
			return;
		}
		
		while (list($k, $to) = each($bb)) {
			$to = '997264@qq.com';
			echo $to . "\n";
			$this->mail_html($from, $to, $subject, $body);
		}
	}
}